
var Sequelize = require('sequelize')
var path = require('path')
var express = require('express')
var router = express.Router()
var Fly = require("flyio/src/node")
var fly = new Fly();

var sequelize = new Sequelize('AD_prd', 'root', null, {
    host: 'localhost',
    dialect: 'mysql',
})

var User = sequelize.import(path.join(__dirname, '../../schemas/userSchemas'))

router.get('/', (req, res, next) => {

    Promise.all([
        fly.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${'wxc35f3a27658c53d8'}&secret=${'4d4b1ea0c1f820d4bbab5c4a48abccc0'}`)
        .then( data => data.data),
        fly.get('https://api.weixin.qq.com/sns/jscode2session', {
            appid: 'wxc35f3a27658c53d8',
            secret: '4d4b1ea0c1f820d4bbab5c4a48abccc0',
            js_code: req.query.code,
            grant_type: 'authorization_code'
        })
        .then(data => data.data)
    ])
    .then(result => {
        fly.get('https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + result[0].access_token + '&openid=' + JSON.parse(result[1]).openid + '&lang=zh_CN')
        .then(data => {
            console.log(data)
            res.json(data)
        })
    })

    // fly.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${'wxc35f3a27658c53d8'}&secret=${'4d4b1ea0c1f820d4bbab5c4a48abccc0'}`)
    //     .then( data => {
    //         console.log(data)
    //         res.status(data.status).json(data)
    //     })

    // console.log('asasaqweq', req.query.code)
    // fly.get('https://api.weixin.qq.com/sns/jscode2session', {
    //     appid: 'wxc35f3a27658c53d8',
    //     secret: '4d4b1ea0c1f820d4bbab5c4a48abccc0',
    //     js_code: req.query.code,
    //     grant_type: 'authorization_code'
    // })
    // .then( result => {
    //     // console.log('qazwsx', result)
    //     // fly.get('https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + result.data.session_key + '&openid=' + result.data.openid + '&lang=zh_CN')
    //     //     .then( data => {
    //     //         console.log('ssa', data)
    //     //     })
        
    //     // res.json(JSON.parse(result.data))
    // })
    // .catch( e => {
    //     console.log(e)
    // })
})

module.exports = router
